package intranet.models;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class LoginModel {
	
	Connection DBConnection=null;
	public LoginModel(){
		DatabaseModel DBModel=new DatabaseModel();
		DBConnection=DBModel.GetConnection();
	}
	public ResultSet LoadUser(){
		ResultSet rs=null;
		try{
			Statement ConStatement=DBConnection.createStatement();
			String sql="SELECT USER_ID,USER_NAME,PASSWORD FROM USER_ACCOUNTS";
			rs=ConStatement.executeQuery(sql);
		}catch(Exception ex){ex.getLocalizedMessage();}
		return rs;
	}
	public boolean verifyUser(String user_name,String pwd){
		boolean result=false;
		int i=0;
		ResultSet rs=null;
		
		try{
			Statement ConStatement=DBConnection.createStatement();
			String sql="SELECT USER_ID,USER_NAME,PASSWORD FROM USER_ACCOUNTS WHERE USER_NAME='"+user_name+"' AND PASSWORD='"+pwd+"'";
			rs=ConStatement.executeQuery(sql);
			while(rs.next()){
				i++;
			}
		}catch(Exception ex){ex.getLocalizedMessage();}
		result=(i==0)?false:true;
		return result;
	}
	
	/* GET USER_ID BY USER_NAME FROM USER_ACCOUNTS */
	public int getUserIdByUserName(String user_name){
		int result=0;
		ResultSet rst=null;
		try{
			Statement ConStatement=DBConnection.createStatement();
			String Sql="SELECT USER_ID,USER_NAME FROM USER_ACCOUNTS WHERE USER_NAME='"+user_name+"'";
			rst=ConStatement.executeQuery(Sql);
			while(rst.next()){
				result=rst.getInt("USER_ID");
			}
		}catch(Exception e){
			e.getLocalizedMessage();
		}
		return result;
	}
}
